package org.dromara.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.system.domain.TradingRecords;
import org.dromara.system.domain.bo.TradingRecordsBo;
import org.dromara.system.domain.bo.TradingRecordsBySnBo;
import org.dromara.system.domain.vo.TradingRecordsVo;
import org.dromara.system.mapper.TradingRecordsMapper;
import org.dromara.system.service.ITradingRecordsService;
import org.dromara.system.service.impl.permission.PermissionBaseService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 交易记录Service业务层处理
 *
 * @author Lion Li
 * @date 2025-07-08
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class TradingRecordsServiceImpl extends ServiceImpl<TradingRecordsMapper, TradingRecords> implements ITradingRecordsService {

 private final PermissionBaseService permissionBaseService;

    /**
     * 查询交易记录
     *
     * @param id 主键
     * @return 交易记录
     */
    @Override
    public TradingRecordsVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 分页查询交易记录列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 交易记录分页列表
     */
    @Override
    public TableDataInfo<TradingRecordsVo> queryPageList(TradingRecordsBo bo, PageQuery pageQuery) {
        permissionBaseService.assignPermission(bo);
        IPage<TradingRecordsVo> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        IPage<TradingRecordsVo> list = baseMapper.findAll(page, bo);
        return TableDataInfo.build(list);
    }

    @Override
    public TableDataInfo<TradingRecordsVo> queryPageListBySn(TradingRecordsBySnBo bo, PageQuery pageQuery) {
        IPage<TradingRecordsVo> page = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        IPage<TradingRecordsVo> list = baseMapper.findAllBySn(page, bo);
        return TableDataInfo.build(list);
    }


    /**
     * 查询符合条件的交易记录列表
     *
     * @param bo 查询条件
     * @return 交易记录列表
     */
    @Override
    public List<TradingRecordsVo> queryList(TradingRecordsBo bo) {
        LambdaQueryWrapper<TradingRecords> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TradingRecords> buildQueryWrapper(TradingRecordsBo bo) {
        LambdaQueryWrapper<TradingRecords> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(TradingRecords::getId);
        lqw.eq(bo.getAmount() != null, TradingRecords::getAmount, bo.getAmount());
        lqw.eq(bo.getTradingTime() != null, TradingRecords::getTradingTime, bo.getTradingTime());
        return lqw;
    }

    /**
     * 新增交易记录
     *
     * @param bo 交易记录
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(TradingRecordsBo bo) {
        TradingRecords add = MapstructUtils.convert(bo, TradingRecords.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改交易记录
     *
     * @param bo 交易记录
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(TradingRecordsBo bo) {
        TradingRecords update = MapstructUtils.convert(bo, TradingRecords.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TradingRecords entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除交易记录信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }


}
